package com.code.lightning.common.convert;

import com.code.lightning.code.mysql.MySqlConvertEnum;
import com.code.lightning.code.oracle.OracleConvertEnum;
import com.code.lightning.common.api.BasicEntityConvert;
import com.code.lightning.common.domain.BasicClassConvertMetaData;
import com.code.lightning.common.domain.BasicFieldConvertMetaData;
import com.code.lightning.common.domain.BasicTableFieldMetaData;
import com.code.lightning.common.domain.BasicTableMetaData;
import com.code.lightning.common.enums.LightingCore;
import com.code.lightning.config.BasicGeneratorConfig;
import com.code.lightning.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Java数据库转实体数据
 * @author LGX_TvT
 * @date 2021-01-15 22:41
 */
@Slf4j
public class JavaEntityConvert implements BasicEntityConvert {

    @Override
    public BasicClassConvertMetaData convertEntity(BasicTableMetaData tableMetaData, List<BasicTableFieldMetaData> fieldMetaDatas, BasicGeneratorConfig config) {
        try {
            // 构建基础类
            BasicClassConvertMetaData classConvertMetaData = new BasicClassConvertMetaData();
            classConvertMetaData.setClassMetaData(tableMetaData);
            classConvertMetaData.setClassComment(tableMetaData.getTableComment() == null ? "" : tableMetaData.getTableComment());
            classConvertMetaData.setClassName(StringUtil.formatByStyle(tableMetaData.getTableName(), config.getNameStyle(), LightingCore.ElementType.CLASS.getValue()));
            classConvertMetaData.setClassPackage(config.getPackageName());
            classConvertMetaData.setGlobalNameStyle(config.getNameStyle());

            // 构建属性
            List<BasicFieldConvertMetaData> fieldConvertMetaDataList = new ArrayList<>();
            List<BasicFieldConvertMetaData> pkList = new ArrayList<>();
            List<String> needImportClassPaths = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(fieldMetaDatas)) {
                fieldMetaDatas.stream().sorted((o1, o2) -> {
                    return Integer.parseInt(o1.getOrdinalPosition()) - Integer.parseInt(o2.getOrdinalPosition());
                }).forEach(fieldMetaData -> {
                    BasicFieldConvertMetaData field = new BasicFieldConvertMetaData();
                    field.setFieldComment(fieldMetaData.getColumnComment() == null ? "" : fieldMetaData.getColumnComment());
                    field.setFieldDefault(fieldMetaData.getColumnDefault());
                    field.setFieldName(StringUtil.formatByStyle(fieldMetaData.getColumnName(), config.getNameStyle(), LightingCore.ElementType.FIELD.getValue()));

                    if (LightingCore.DataBaseCategory.MYSQL.getName().equals(config.getDbCategory())) {
                        MySqlConvertEnum type = MySqlConvertEnum.getEnumByMySqlType(fieldMetaData.getDataType().toUpperCase());
                        field.setFieldType(type.getJavaType());
                        field.setFieldFullType(type.getJavaTypeClassName());
                        field.setNeedImport(LightingCore.TypeRequireImport.getIsRequireImport(type.getJavaTypeClassName()));
                    } else if (LightingCore.DataBaseCategory.ORACLE.getName().equals(config.getDbCategory())){
                        OracleConvertEnum type = OracleConvertEnum.getEnumByOracleType(fieldMetaData.getDataType().toUpperCase());
                        field.setFieldType(type.getJavaType());
                        field.setFieldFullType(type.getJavaTypeClassName());
                        field.setNeedImport(LightingCore.TypeRequireImport.getIsRequireImport(type.getJavaTypeClassName()));
                    }

                    field.setPk("PRI".equalsIgnoreCase(fieldMetaData.getColumnKey()));
                    field.setFieldMetaData(fieldMetaData);
                    field.setFieldPosition(new Integer(fieldMetaData.getOrdinalPosition()));
                    fieldConvertMetaDataList.add(field);

                    // 主键
                    if (field.getPk()) {
                        pkList.add(field);
                    }
                    // 导包
                    if (field.getNeedImport()) {
                        needImportClassPaths.add(field.getFieldFullType());
                    }
                });
            }
            classConvertMetaData.setFields(fieldConvertMetaDataList);
            classConvertMetaData.setPk(pkList);
            classConvertMetaData.setImportClassPaths(needImportClassPaths);
            return classConvertMetaData;
        } catch (Exception e) {
            log.error("Convert Error: {}", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
}
